﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

/// <summary>
/// Summary description for Email
/// </summary>
namespace Util
{
    public class Email
    {

        public string from { set; get; }
        public List<string> to { set; get; }
        public List<string> bcc { set; get; }
        public List<string> cc { set; get; }
        public string subject { set; get; }
        public string message { set; get; }
        public List<Attachment> attachments { set; get; }

        public Email()
        {
            this.to = new List<string>();
            this.cc = new List<string>();
            this.bcc = new List<string>();
            this.attachments = new List<Attachment>();
        }

        public bool Enviar()
        {
            MailMessage myMessage = new MailMessage();
            SmtpClient myMail = new SmtpClient();

            //Inicia retorno
            Boolean strRetornoEnvio;

            //Processa envio de e-mail
            try
            {
                foreach (string strEmail in this.to)
                {
                    myMessage.To.Add(new MailAddress(strEmail));
                }

                foreach (string strEmail in this.cc)
                {
                    myMessage.CC.Add(new MailAddress(strEmail));
                }

                foreach (string strEmail in this.bcc)
                {
                    myMessage.Bcc.Add(new MailAddress(strEmail));
                }

                foreach (Attachment item in this.attachments)
                {
                    myMessage.Attachments.Add(item);
                }

                myMessage.From = new MailAddress(System.Configuration.ConfigurationSettings.AppSettings["emailFrom"].ToString());
                myMessage.Priority = MailPriority.Normal;
                myMessage.IsBodyHtml = true;
                myMessage.Subject = this.subject;
                myMessage.Body = this.message;

                //Para evitar problemas com caracteres especiais (configura o Charset)
                myMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                myMessage.BodyEncoding = System.Text.Encoding.UTF8;

                myMail.Host = System.Configuration.ConfigurationSettings.AppSettings["ServidorSMTP"].ToString();

                myMail.Send(myMessage);

                strRetornoEnvio = true;
            }
            catch (Exception ex)
            {
                strRetornoEnvio = false;
            }

            return strRetornoEnvio;
        }
    }
}